﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Joyintech.Common;

namespace Joyintech.Admin.inpiao365.core
{
    public partial class uploadImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string ext = Path.GetExtension(this.FileUpload1.FileName).ToLower();

            if (this.CheckUploadFileType(ext))
            {
                string fullName = this.FileUpload1.FileName;
                string FileName = fullName.Substring(fullName.LastIndexOf('\\') + 1);
                string FilePath = base.Server.MapPath("/Uploads/") + FileName;
                if (base.Request.QueryString["path"] != null)
                {
                    FilePath = base.Server.MapPath("/" + base.Request.QueryString["path"].ToString() + "/") + FileName;
                }
                if (!Directory.Exists(base.Server.MapPath("/" + base.Request.QueryString["path"].ToString() + "/")))
                {
                    Directory.CreateDirectory(base.Server.MapPath("/" + base.Request.QueryString["path"].ToString() + "/"));
                }
                
                this.FileUpload1.SaveAs(FilePath);

                if (base.Request.QueryString["path"] != null)
                {
                    base.Response.Redirect("selectAttachment.aspx?path=" + base.Request.QueryString["path"].ToString());
                }
                else
                {
                    base.Response.Redirect("selectAttachment.aspx");
                }
            }
            else
            {
                MessageBox.Show(this, "只允许上传扩展名为：" + ConfigHelper.GetConfigString("UploadFileType") + "格式的文件！");
            }
        }

        private bool CheckUploadFileType(string extName)
        {
            string[] filetype = ConfigHelper.GetConfigString("UploadFileType").Split(new char[] { ',' });

            foreach (string ext in filetype)
            {
                if (extName.ToLower() == "." + ext.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
    }
}